package org.jeecg.jxc.base.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.jxc.base.customer.entity.Customer;
import org.jeecg.jxc.base.customer.service.CustomerService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @description:
 * @company: yaorange
 * @author: Freya
 * @version: 1.0
 * @create: 2022-04-26
 */
@Api(tags = "客户管理",description = "客户管理接口api")
@RestController
@RequestMapping("/customer")
@Slf4j
public class CustomerController {
    @Resource
    private CustomerService customerService;
//http://localhost:8080/jeecg-boot/customer/list?customerName=家乐福&pageNo=1&pageSize=2
    @GetMapping(value = "/list")
    @ApiOperation(value = "客户列表分页模糊查询",notes = "获取所有客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "第几页",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "展示数据条数",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "customerName",value = "客户名称",required = false,dataType = "String"),
    })

    public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="2") Integer pageSize,
                                   String customerName) {
        //模糊查询的分页
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();


        if(!StringUtils.isEmpty(customerName)){
            queryWrapper.like("customer_name",customerName);
        }


        Page<Customer> page = new Page<>(pageNo, pageSize);
        IPage<Customer> pageList = customerService.page(page, queryWrapper);
        return Result.ok(pageList);
    }
//http://localhost:8080/jeecg-boot/customer/add
    //{"customerName":"demoData","contacts":"demoData","phoneNumber":"demoData","remarks":"demoData"}
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增客户",notes ="根据Customer对象创建新客户" )
    @ApiImplicitParam(name = "customer",value = "客户实体对象",required = true,dataType = "Customer")
    public Result<?> add(@RequestBody Customer customer) {
        customerService.save(customer);
        return Result.ok("新增成功！");
    }
//http://localhost:8080/jeecg-boot/customer/update
    //{"customerId":"6","customerName":"demoData2","contacts":"demoData","phoneNumber":"demoData","remarks":"demoData"}
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改客户信息",notes = "根据传过来的对象实体修改客户信息")
    @ApiImplicitParam(name = "customer",value = "客户实体",dataType = "Customer")
    public Result<?> edit(@RequestBody Customer customer) {
        customerService.updateById(customer);
        return Result.ok("修改成功!");
    }
//http://localhost:8080/jeecg-boot/customer/deleteLots?ids=5
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "批量删除客户",notes = "根据url的id字符串转为数组来指定删除对象")
    @ApiImplicitParam(name = "ids",value = "客户的id数组",dataType = "String")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.customerService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }



}